package org.huel.backend.mapper;

import org.apache.ibatis.annotations.*;
import org.huel.backend.domain.entity.CourseSimilarity;

import java.util.List;

@Mapper
public interface CourseSimilarityMapper {

    /**
     * 查询与指定课程相似的课程列表
     * @param courseName 课程名称
     * @return 相似课程列表
     */
    @Select("SELECT * FROM course_similarity WHERE course1 = #{courseName} OR course2 = #{courseName} ORDER BY similarity_score DESC")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "course1", column = "course1"),
            @Result(property = "course2", column = "course2"),
            @Result(property = "similarityScore", column = "similarity_score"),
            @Result(property = "lastUpdated", column = "last_updated")
    })
    List<CourseSimilarity> selectSimilarCourses(@Param("courseName") String courseName);

    /**
     * 查询两门课程之间的相似度
     * @param course1 课程1名称
     * @param course2 课程2名称
     * @return 课程相似度记录
     */
    @Select("SELECT * FROM course_similarity WHERE (course1 = #{course1} AND course2 = #{course2}) OR (course1 = #{course2} AND course2 = #{course1})")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "course1", column = "course1"),
            @Result(property = "course2", column = "course2"),
            @Result(property = "similarityScore", column = "similarity_score"),
            @Result(property = "lastUpdated", column = "last_updated")
    })
    CourseSimilarity selectCoursePairSimilarity(@Param("course1") String course1, @Param("course2") String course2);

    /**
     * 查询相似度高于指定阈值的课程对
     * @param threshold 相似度阈值
     * @return 高相似度课程对列表
     */
    @Select("SELECT * FROM course_similarity WHERE similarity_score >= #{threshold} ORDER BY similarity_score DESC")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "course1", column = "course1"),
            @Result(property = "course2", column = "course2"),
            @Result(property = "similarityScore", column = "similarity_score"),
            @Result(property = "lastUpdated", column = "last_updated")
    })
    List<CourseSimilarity> selectHighSimilarityCourses(@Param("threshold") float threshold);

    /**
     * 插入课程相似度记录
     * @param courseSimilarity 课程相似度实体
     * @return 影响的行数
     */
    @Insert("INSERT INTO course_similarity (course1, course2, similarity_score) VALUES (#{course1}, #{course2}, #{similarityScore})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertCourseSimilarity(CourseSimilarity courseSimilarity);

    /**
     * 更新课程相似度记录
     * @param courseSimilarity 课程相似度实体
     * @return 影响的行数
     */
    @Update("UPDATE course_similarity SET similarity_score = #{similarityScore} WHERE id = #{id}")
    int updateCourseSimilarity(CourseSimilarity courseSimilarity);
}